package com.cn.kabalifeuser.ui.order.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.order.bean.OrderDetailsAfterSaleBean;
import com.cn.kabalifeuser.utils.GlideUtil;
import com.cn.kabalifeuser.utils.RxToast;
import com.cn.kabalifeuser.utils.SoftHideKeyBoardUtil;
import com.flyco.roundview.RoundTextView;
import com.makeramen.roundedimageview.RoundedImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/9/25.
 * function: 售后详情
 */
public class MallOrderAfterSaleDetailsActivity extends IBaseActivity<MallOrderAfterSaleDetailsView, MallOrderAfterSaleDetailsPresenter> implements MallOrderAfterSaleDetailsView {


    Bundle bundle;
    int id;
    @BindView(R.id.tv_start)
    TextView tvStart;
    @BindView(R.id.tv_top_created_at)
    TextView tvTopCreatedAt;
    @BindView(R.id.img_top_img_2)
    ImageView imgTopImg2;
    @BindView(R.id.tv_top_num_2)
    TextView tvTopNum2;
    @BindView(R.id.tv_start_2)
    TextView tvStart2;
    @BindView(R.id.img_top_img_3)
    ImageView imgTopImg3;
    @BindView(R.id.tv_top_num_3)
    TextView tvTopNum3;
    @BindView(R.id.tv_start_3)
    TextView tvStart3;
    @BindView(R.id.good_img)
    RoundedImageView goodImg;
    @BindView(R.id.tv_good_title)
    TextView tvGoodTitle;
    @BindView(R.id.tv_spec_key)
    TextView tvSpecKey;
    @BindView(R.id.tv_good_money)
    TextView tvGoodMoney;
    @BindView(R.id.tv_good_num)
    TextView tvGoodNum;
    @BindView(R.id.tv_after_reason)
    TextView tvAfterReason;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_created_at)
    TextView tvCreatedAt;
    @BindView(R.id.tv_out_refund_no)
    TextView tvOutRefundNo;
    @BindView(R.id.tv_b_start)
    TextView tvBStart;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.et_express_name)
    EditText etExpressName;
    @BindView(R.id.et_express_num)
    EditText etExpressNum;
    @BindView(R.id.tv_btn_next)
    RoundTextView tvBtnNext;
    @BindView(R.id.ll_start)
    LinearLayout ll_start;


    @Override
    protected MallOrderAfterSaleDetailsPresenter createPresenter() {
        return new MallOrderAfterSaleDetailsPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_aftersale_details;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        SoftHideKeyBoardUtil.assistActivity(this);
        setTitleBar("售后详情");
        bundle = getIntent().getExtras();
        id = bundle.getInt("id");
        mPresenter.onOrderDetailsData(LoginHelper.getLonginData().getResult(), id);
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onOrderDetailsSuccess(OrderDetailsAfterSaleBean bean) {

        GlideUtil.ImageLoad(this, bean.getResult().getGood_img(), goodImg);
        tvGoodTitle.setText(bean.getResult().getGood_title());
        tvGoodNum.setText("x " + bean.getResult().getGood_num());
        tvGoodMoney.setText(bean.getResult().getMoney());
        tvSpecKey.setText(bean.getResult().getSpec_key());
        tvAfterReason.setText(bean.getResult().getAfter_reason());
        tvMoney.setText("¥" + bean.getResult().getMoney());
        tvCreatedAt.setText(bean.getResult().getCreated_at());
        tvTopCreatedAt.setText(bean.getResult().getCreated_at());
        tvOutRefundNo.setText(bean.getResult().getOut_refund_no());


        if (bean.getResult().getStatus() == 4) {
            ll_start.setVisibility(View.GONE);
            tvStart.setText("商家已拒绝");
            imgTopImg2.setImageResource(R.drawable.yuan_w);
            tvTopNum2.setText("2");
            tvTopNum2.setTextColor(getResources().getColor(R.color.text_dddddd));
            imgTopImg3.setImageResource(R.drawable.yuan_w);
            tvStart3.setText(bean.getResult().getAfter_sale_type() == 1 ? "已退款" : "已换货");
            tvTopNum3.setText("3");
            tvTopNum3.setTextColor(getResources().getColor(R.color.text_dddddd));
            tvBtnNext.setVisibility(View.GONE);

        } else if (bean.getResult().getStatus() == 0) {
            ll_start.setVisibility(View.GONE);
            imgTopImg2.setImageResource(R.drawable.yuan_w);
            tvTopNum2.setText("2");
            tvTopNum2.setTextColor(getResources().getColor(R.color.text_dddddd));

            imgTopImg3.setImageResource(R.drawable.yuan_w);
            tvStart3.setText(bean.getResult().getAfter_sale_type() == 1 ? "已退款" : "已换货");
            tvTopNum3.setText("3");
            tvTopNum3.setTextColor(getResources().getColor(R.color.text_dddddd));
            tvBtnNext.setVisibility(View.GONE);
        } else if (bean.getResult().getStatus() == 1) {
            ll_start.setVisibility(View.VISIBLE);
            imgTopImg2.setImageResource(R.drawable.yuan);
            tvTopNum2.setText("2");
            tvTopNum2.setTextColor(getResources().getColor(R.color.main_white));
            imgTopImg3.setImageResource(R.drawable.yuan_w);
            tvStart3.setText(bean.getResult().getAfter_sale_type() == 1 ? "已退款" : "已换货");
            tvTopNum3.setText("3");
            tvTopNum3.setTextColor(getResources().getColor(R.color.text_dddddd));
            tvBStart.setText(bean.getResult().getAfter_sale_type() == 1 ? "退款信息" : "换货信息");
            tvAddress.setText(bean.getResult().getAddress() + "\n" + bean.getResult().getConsignee() + "   " + bean.getResult().getTel());
            if (!TextUtils.isEmpty(bean.getResult().getExpress_name())) {
                etExpressName.setEnabled(false);
                tvBtnNext.setVisibility(View.GONE);
                etExpressName.setText(bean.getResult().getExpress_name());
            } else {
                tvBtnNext.setVisibility(View.VISIBLE);
                etExpressName.setEnabled(true);
            }
            if (!TextUtils.isEmpty(bean.getResult().getExpress_num())) {
                etExpressNum.setEnabled(false);
                etExpressNum.setText(bean.getResult().getExpress_num());
            } else {
                etExpressNum.setEnabled(true);
            }
        } else if (bean.getResult().getStatus() == 3) {
            ll_start.setVisibility(View.VISIBLE);
            tvBtnNext.setVisibility(View.GONE);
            imgTopImg2.setImageResource(R.drawable.yuan);
            tvTopNum2.setText("2");
            tvTopNum2.setTextColor(getResources().getColor(R.color.main_white));

            imgTopImg3.setImageResource(R.drawable.yuan);
            tvStart3.setText(bean.getResult().getAfter_sale_type() == 1 ? "已退款" : "已换货");
            tvTopNum3.setText("3");
            tvTopNum3.setTextColor(getResources().getColor(R.color.main_white));
            tvBStart.setText(bean.getResult().getAfter_sale_type() == 1 ? "退款信息" : "换货信息");
            tvAddress.setText(bean.getResult().getAddress() + "\n" + bean.getResult().getConsignee() + "   " + bean.getResult().getTel());

            if (!TextUtils.isEmpty(bean.getResult().getExpress_name())) {
                etExpressName.setEnabled(false);
                etExpressName.setText(bean.getResult().getExpress_name());
            } else {
                etExpressName.setEnabled(true);
            }
            if (!TextUtils.isEmpty(bean.getResult().getExpress_num())) {
                etExpressNum.setEnabled(false);
                etExpressNum.setText(bean.getResult().getExpress_num());
            } else {
                etExpressNum.setEnabled(true);
            }
        }


    }

    @Override
    public void onAfterSuccess(MsgBean bean) {

        RxToast.success(bean.getMsg());
        mPresenter.onOrderDetailsData(LoginHelper.getLonginData().getResult(), id);

    }


    @OnClick(R.id.tv_btn_next)
    public void onViewClicked() {
        mPresenter.onAfterData(LoginHelper.getLonginData().getResult(), id, etExpressName.getText().toString().trim(), etExpressNum.getText().toString().trim());
    }
}
